package cn.clq.basic.controller;

import cn.clq.basic.dto.LoginDto;
import cn.clq.basic.dto.WeChatDto;
import cn.clq.basic.exception.BusinessException;
import cn.clq.basic.service.ILoginService;
import cn.clq.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author clqlongbao
 * @version 1.0
 * @description: TODO
 * @date 2022/6/18 17:10
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;


    /**
     * @description: 后台员工账号密码登录
     * @param:
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/18 17:15
     */
    @RequestMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }


    /**
     * @description: 微信扫码登陆接口
     * @param: dto
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/19 15:04
     */
    @RequestMapping("/wechat")
    public AjaxResult wechat(@RequestBody WeChatDto dto){
        try {
            return loginService.wechat(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }

    /**
     * @description: 微信账号绑定接口
     * @param: dto
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/19 19:56
     */
    @RequestMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WeChatDto dto){
        try {
            //登录成功之后，service返回token  + logininfo
            Map<String, Object> map = loginService.binder(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }
}
